﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.Table;

namespace map.member
{
    public partial class ReceiveItemAddress : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                showCreatedDiv(true);
                BindMainDataList();
                InitAreaDropDownLists();
            }
        }


        #region 辅助方法    
        private void InitAreaDropDownLists()
        {
            this.ProvinceDropDownList.Items.Clear();
            this.ProvinceDropDownList.Items.Add(new ListItem("请您选择省", ""));
            省 province = 省.Find("1=1");
            this.ProvinceDropDownList.DataSource = province.MyTable;
            this.ProvinceDropDownList.DataBind();
            this.ProvinceDropDownList.SelectedIndex = 0;
            ProvinceDropDownList_SelectedIndexChanged(this.ProvinceDropDownList, EventArgs.Empty);
        }

        private void showCreatedDiv(bool isShow)
        {
            this.modifyDH2.Style["display"] = isShow ? "none" : "";
            this.modifyD.Style["visibility"] = isShow ? "hidden" : "visible";
            this.createD.Style["display"] = isShow ? "" : "none";
            this.createDH2.Style["visibility"] = isShow ? "visible" : "hidden";
            //this.modifyDH2.Visible  = !isShow ;
            //this.modifyD.Visible = !isShow;
            //this.createD.Visible = isShow;
            //this.createDH2.Visible = isShow;
        }

        private void BindMainDataList()
        {
            cbpostaddress address = cbpostaddress.Find("cbuserid='"+AliApi.appUserId +"'");
            this.MainDataList.DataSource = address.MyTable;
            this.MainDataList.DataBind();
        }
        #endregion

        #region 事件
        protected void CreateImg_ServerClick(object sender, EventArgs e)
        {
            if (AliApi.appUserId != string.Empty)
            {
                cbpostaddress address = cbpostaddress.Find("1=2");
                address.New();
                address.cbuserid.Value = AliApi.appUserId;
                address.省编码.Value = this.ProvinceDropDownList.SelectedValue;
                address.县编码.Value = this.CityDropDownList.SelectedValue;
                address.区编码.Value = this.DisctictDropDownList.SelectedValue;
                address.区域编码.Value = this.AreaDropDownList.SelectedValue;
                address.邮政编码.Value = this.PostCodeTextBox.Text;
                address.地址.Value = this.DetailAddressTextBox.Text;
                address.cbname.Value = ReceiveNameTextBox.Text;
                address.Phone.Value = PhoneTextBox.Text;
                address.PhoneLocal.Value = PhoneLocalTextBox.Text;
                address.PhoneMobile.Value = PhoneMobileTextBox.Text;
                address.Insert();
                BindMainDataList();
                oprResultDiv.InnerHtml = "新建收货地址成功了！";
            }
            else 
            {
                oprResultDiv.InnerHtml = "新建收货地址失败了！";
            }
        }

        protected void CreateAddressAnchor_ServerClick(object sender, EventArgs e)
        {
            this.currentRecidHidden.Value = "";
            showCreatedDiv(true);
            this.ProvinceDropDownList.SelectedValue = "";
            this.ProvinceDropDownList_SelectedIndexChanged(ProvinceDropDownList, EventArgs.Empty);
            this.PostCodeTextBox.Text = "";
            this.DetailAddressTextBox.Text = "";
            ReceiveNameTextBox.Text = "";
            PhoneTextBox.Text = "";
            PhoneLocalTextBox.Text = "";
            PhoneMobileTextBox.Text = "";
            oprResultDiv.InnerHtml = "";
        }

        protected void ModifyImg_ServerClick(object sender, EventArgs e)
        {
            if (AliApi.appUserId != string.Empty)
            {
                string currentRecid = currentRecidHidden.Value;
                cbpostaddress address = cbpostaddress.Find("recid=" + currentRecid);
                if (address.Count > 0)
                {
                    address.cbuserid.Value = AliApi.appUserId;
                    address.省编码.Value = this.ProvinceDropDownList.SelectedValue;
                    address.县编码.Value = this.CityDropDownList.SelectedValue;
                    address.区编码.Value = this.DisctictDropDownList.SelectedValue;
                    address.区域编码.Value = this.AreaDropDownList.SelectedValue;
                    address.邮政编码.Value = this.PostCodeTextBox.Text;
                    address.地址.Value = this.DetailAddressTextBox.Text;
                    address.cbname.Value = ReceiveNameTextBox.Text;
                    address.Phone.Value = PhoneTextBox.Text;
                    address.PhoneLocal.Value = PhoneLocalTextBox.Text;
                    address.PhoneMobile.Value = PhoneMobileTextBox.Text;
                    address.Modify();
                    BindMainDataList();

                    oprResultDiv.InnerHtml = "您的收货地址修改成功了！";
                }
                else
                {
                    MessageBox.Show(this.Page, "对不起，没有找到您要修改的地址！"); 
                    oprResultDiv.InnerHtml = "您的收货地址修改失败了！";
                }
            }
            
        }

        protected void MainDataList_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (AliApi.appUserId != string.Empty)
            {
                string recid = e.CommandArgument.ToString();
                cbpostaddress address = cbpostaddress.Find("recid=" + recid);
                if (address.Count > 0)
                {
                    if (e.CommandName == "Edit")
                    {
                        this.currentRecidHidden.Value = recid;
                        showCreatedDiv(false);
                        this.ProvinceDropDownList.SelectedValue=address.省编码.Value ;
                        ProvinceDropDownList_SelectedIndexChanged(ProvinceDropDownList, EventArgs.Empty);
                        this.CityDropDownList.SelectedValue =address.县编码.Value ;
                        CityDropDownList_SelectedIndexChanged(CityDropDownList, EventArgs.Empty);
                        this.DisctictDropDownList.SelectedValue=address.区编码.Value ;
                        DisctictDropDownList_SelectedIndexChanged(DisctictDropDownList, EventArgs.Empty);
                        this.AreaDropDownList.SelectedValue=address.区域编码.Value ;
                        this.PostCodeTextBox.Text=address.邮政编码.Value ;
                        this.DetailAddressTextBox.Text=address.地址.Value ;
                        ReceiveNameTextBox.Text=address.cbname.Value ;
                        PhoneTextBox.Text=address.Phone.Value ;
                        PhoneLocalTextBox.Text=address.PhoneLocal.Value ;
                        PhoneMobileTextBox.Text=address.PhoneMobile.Value;
                    }

                    if (e.CommandName == "Delete")
                    {
                        this.currentRecidHidden.Value = "";
                        address.Delete("recid=" + recid);
                        BindMainDataList();
                    }
                }
            }
            else
            {
                MessageBox.ShowAndRedirect(this.Page, "对不起，您还未登录！", "login.aspx?redirecturl=" + this.Request.Url.ToString());
            }
        }


        #endregion


        #region 地区下拉框事件
        protected void CityDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DisctictDropDownList.Items.Clear();
            this.DisctictDropDownList.Items.Add(new ListItem ("请选择区",""));
            if (this.CityDropDownList.SelectedValue != string.Empty)
            {
                区 disctict = 区.Find("县编码='"+this.CityDropDownList.SelectedValue +"'");
                this.DisctictDropDownList.DataSource = disctict.MyTable;
                this.DisctictDropDownList.DataBind();
            }
            this.DisctictDropDownList.SelectedIndex = 0;
            DisctictDropDownList_SelectedIndexChanged(this.DisctictDropDownList, EventArgs.Empty);
        }

        protected void ProvinceDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.CityDropDownList.Items.Clear();
            this.CityDropDownList.Items.Add(new ListItem("请选择市",""));
            if (this.ProvinceDropDownList.SelectedValue != string.Empty)
            {
                县 city = 县.Find("省编码='"+this.ProvinceDropDownList.SelectedValue +"'");
                this.CityDropDownList.DataSource = city.MyTable;
                this.CityDropDownList.DataBind();
            }
            this.CityDropDownList.SelectedIndex = 0;
            CityDropDownList_SelectedIndexChanged(this.CityDropDownList, EventArgs.Empty);
        }

        protected void DisctictDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.AreaDropDownList.Items.Clear();
            this.AreaDropDownList.Items.Add(new ListItem ("请选择区域",""));
            if (this.DisctictDropDownList.SelectedValue != string.Empty)
            {
                区域 area = 区域.Find("县编码='"+this.CityDropDownList .SelectedValue +"' and 区编码='"+this.DisctictDropDownList.SelectedValue +"'");
                this.AreaDropDownList.DataSource = area.MyTable;
                this.AreaDropDownList.DataBind();
            }
            this.AreaDropDownList.SelectedIndex = 0;
        }
        #endregion

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (args.Value.Length <= 5)
                args.IsValid = false;
            else
                args.IsValid = true;
        }
    }
}
